﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiArte.Data.Catalog
{
    public class CategoryDal : BaseDal
    {
        /// <summary>
        /// Deletes all categories
        /// </summary>
        public void DeleteAll()
        {
            using (var command = new Npgsql.NpgsqlCommand("DELETE FROM category"))
                PerformCommand(command);
        }

        /// <summary>
        /// Inserts a new category
        /// </summary>
        /// <param name="code">The unique category code</param>
        /// <param name="path">The category path</param>
        /// <returns>The inserted category id</returns>
        public int Insert(String code, String path)
        {
            using (var command = new Npgsql.NpgsqlCommand("INSERT INTO category(category_id, code, path) VALUES " +
                "(nextval('category_id_sequence'), :code, :path) RETURNING category_id"))
            {
                command.Parameters.AddWithValue("code", code);
                command.Parameters.AddWithValue("path", path);

                return PerformInsertCommandAndGetReturningValue(command);
            }
        }
    }
}
